<?php
class m_relasi_kakak_pembina extends Model{
    var $table = "relasi_pembina";
    function relasi_kakak_pembina(){
        parent::Model();
    }
    function getRelasiKakakPembinaAdikAsuh(){
        $qPembina = $this->db->query("select jk.id_member,m.nama from jenis_keanggotaan jk 
        join member m on jk.id_member = m.id
        where jk.id_jenis_member = 4");
        $result = array();
        if($qPembina->num_rows() > 0){
            $i = 0;
            foreach ($qPembina->result_array() as $row){
                $result[$i]['kakakPembina'] = $row;
                $qAdikAsuh = $this->db->query("select rp.*,a.nama as adik_asuh,s.nama as sekolah,kls.nama as kelas,tp.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sms.nama as semester from relasi_pembina rp 
                        join adik_asuh a on rp.id_adik_asuh = a.id
                        join dinamis_adik_asuh da on da.id_adik_asuh = a.id
                        join sekolah s on da.id_sekolah = s.id
                        join kelas kls on da.id_kelas = kls.id
                        join tingkat_pendidikan tp on kls.id_tingkat_pendidikan = tp.id
                        join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                        join semester sms on ta.id_semester = sms.id
                        where rp.id_member = '$row[id_member]' and da.status = '1'");
                if($qAdikAsuh->num_rows() > 0){
                    foreach ($qAdikAsuh->result_array() as $rows){
                        $result[$i]['adik_asuh'][] = $rows;
                    }
                }else $result[$i]['adik_asuh'][] = array();
                $i++;
            }
        }
        return $result;
    }
    function getRelasiKakakPembina($idKakakPembina = NULL,$id = NULL){
        if($idKakakPembina != NULL){
            $where = "and rp.id_member = '$idKakakPembina'";
        }else if($id != NULL){
            $where = "and rp.id = '$id'";
        }
        $query = $this->db->query("select m.nama as pembina,m.id as id_pembina,dm.alamat as alamat_pembina,dm.no_telp as telp_pembina,rp.id,a.nama as adik_asuh,a.tgl_lahir,a.jenis_kelamin,k.nama as kelas,a.id as id_adik_asuh,tk.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sm.nama as semester,da.alamat,s.nama as sekolah from relasi_pembina rp 
                                  join adik_asuh a on rp.id_adik_asuh = a.id
                                  join dinamis_adik_asuh da on da.id_adik_asuh = a.id 
                                  join member m on rp.id_member = m.id
                                  join dinamis_member dm on dm.id_member = m.id
                                  join sekolah s on da.id_sekolah = s.id 
                                  join kelas k on da.id_kelas = k.id
                                  join tingkat_pendidikan tk on s.id_tingkat_pendidikan = tk.id
                                  join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                                  join semester sm on ta.id_semester = sm.id
                                  where da.status = '1' and dm.status = '1' $where");
        if($query->num_rows() > 0){
            $result =  $query->result_array();
        }else $result =  array();
        
        return $result;
    }
    function searchKakakPembina($nama){
        $query = $this->db->query("select jk.id_member,m.nama as member,dm.alamat,dm.no_telp from jenis_keanggotaan jk 
        join member m on jk.id_member = m.id
        join dinamis_member dm on dm.id_member = m.id
        where jk.id_jenis_member = 4 and dm.status = '1' and locate('$nama',m.nama) > 0 order by locate('$nama',m.nama)");
        if($query->num_rows() > 0){
            $result =  $query->result_array();
        }else $result =  array();
        
        return $result;
    }
    function searchRelasiKakakPembina($nama){
        $query = $this->db->query("select a.id as id_adik_asuh,a.nama as adik_asuh,da.alamat,s.nama as sekolah from adik_asuh a
                                  join dinamis_adik_asuh da on da.id_adik_asuh = a.id
                                  join sekolah s on da.id_sekolah = s.id
                                  where a.id not in (select id_adik_asuh from relasi_pembina) and da.status = '1' and locate('$nama',a.nama) > 0 order by locate('$nama',a.nama)");
        if($query->num_rows() > 0){
            $result =  $query->result_array();
        }else $result =  array();
        
        return $result;
    }
    function addRelasiKakakPembina($data){
        $insert = $this->db->insert($this->table,$data);
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function editRelasiKakakPembina($data){
        $this->db->where('id',$data['id']);
        $update = $this->db->update($this->table,$data);
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function deleteRelasiKakakPembina($id){
        $delete = $this->db->delete($this->table,array('id'=>$id));
        if(!$delete){
            return FALSE;
        }else return TRUE;
    }
}
?>
